package com.lkm.market2_0.m_imple.steelyardserver.server.sendtask;

import com.lkm.frame.P;
import com.lkm.frame.task2.ITaskExecutor;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;



public class SetWifiConfigTask extends AbsSendWriteTask {
    public SetWifiConfigTask(ITaskExecutor<Object[], Void, Boolean> taskExecutor, TaskManager mTaskManager, IHardwareControler bluetoothSocket, ReceiveTask mReceiveTask, String wifiName, String wifiPass,String wifiModuleBaudRate, String ip, String port, String wifiModel) {
        super(taskExecutor, SetWifiConfigTask.class.getName(), mTaskManager, bluetoothSocket, mReceiveTask);
        setParam(new Object[]{wifiName,wifiPass,wifiModuleBaudRate,ip,port,wifiModel});
    }


    byte[] data =null;
    @Override
    protected byte onGetRequestCMD(int[] outDataSize) {
        String wifiName=(String)getParam()[0];
        String wifiPass=(String)getParam()[1];
        
        String wifiModuleBaudRate=(String)getParam()[2];
        String ip=(String)getParam()[3];
        String port=(String)getParam()[4];
        
        String wifiModel = (String)getParam()[5];


        try {
//            +++；AT+WMODE=STA；AT+WSTA=market2.0,ast12345678；AT+UART=115200,1,NONE；AT+SOCK=TCPC,192.168.43.1,47111；
            String title="+++；AT+WMODE=STA；AT+WSTA="+wifiName+","+wifiPass+"；AT+UART="+wifiModuleBaudRate+",1,NONE；AT+SOCK=TCPC,"+ip+","+port+"；";



//            "AT+NETP=TCP,CLIENT,47111,192.168.43.1\r\n"



            if("new".equals(wifiModel)){
            title = "a";
            title += "；";
            title += String.format("AT+WSTA=%s,%s\r\n",wifiName,wifiPass);
            title += "；";
            title += String.format("AT+NETP=TCP,CLIENT,%s,%s\r\n", port, ip);
            title += "；";

            title += "AT+SOCKB=NONE\r\n";
            title += "；";
            title += "AT+Z";
            title += "；";
            }

            data=title.getBytes("gbk");;
        } catch (Exception e) {
            data=null;
        }
        outDataSize[0]=data==null?0:data.length;

        return 0x0B;
    }

    @Override
    protected byte[] onGetSendDataItem(int index) {
        if(index>0)return null;
        return data;
    }


    @Override
    public void onPreExecute() {
        super.onPreExecute();
        if(P.isDebug()) {
            P.p("=======开始设置wifi");
            if(UIP.isPrint()){
                UIP.p("Send设置wifi：开始",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onCancel() {
        super.onCancel();
        if(P.isDebug()) {
            P.p("=======设置wifi取消了");
            if(UIP.isPrint()){
                UIP.p("Send设置wifi：取消了",mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
    @Override
    public void onFinish(Boolean aBoolean) {
        super.onFinish(aBoolean);

        if(P.isDebug()) {
            P.p("======设置wifi是否成功："+aBoolean);
            if(UIP.isPrint()){
                UIP.p("Send设置wifi：成功？"+aBoolean,mReceiveTask==null?"":mReceiveTask.steelyardMac);
            }
        }
    }
}